function editSpec(sId){
	params = {spec: sId}
	url = "calculator";
	show = "#/"+url;
	loadURL(url, params, show);
}
function deleteSpec(sId){
	$.post("php/deleteSpec.php", {specId: sId},
		function(response, status, xhr){
			if(response.status == "success"){
				loadSpecList('');
			}else{
				var msg = "Sorry but there was an error: ";
				$("#spec-list").html(msg + xhr.status + " " + xhr.statusText);
			}
		   }, "json");
}

function publishSpec(sId){
	
	var content = $('#spec-list').html();
	$('#spec-list').html('<div class=\"center\"><img class=\"spec-list-loader-image\" src=\"media/loader.gif\"></img></div>');
	$.post("php/publishSpec.php", {specId: sId, action: "Y"},
	function(data){
		if(data.status == "success"){
			loadSpecList('');
			//$('#publish-link_'+sId).html("<a class='publish-link font-10 fl-right' name='no' style='cursor:pointer;'>UnPublish</a>");
		}else{
			$('#spec-list').html(content);
			var msg = "Sorry but there was an error: ";
			$("#spec-detail-content").html(msg + xhr.status + " " + xhr.statusText);
		}
	   }, "json");

}

function unPublishSpec(sId){

	var content = $('#spec-list').html();
	$('#spec-list').html('<div class=\"center\"><img class=\"spec-list-loader-image\" src=\"media/loader.gif\"></img></div>');
	$.post("php/publishSpec.php", {specId: sId, action: "N"},
		function(data){
			if(data.status == "success"){
				loadSpecList('');
				//$('#publish-link_'+sId).html("<a id='' class='publish-link font-10 fl-right' name='no' style='cursor:pointer;'>Publish</a>");
			}else{
				$('#spec-list').html(content);
				var msg = "Sorry but there was an error: ";
				$("#spec-detail-content").html(msg + xhr.status + " " + xhr.statusText);
			}
		   }, "json");
}

function getUser(name){
	loadURL("user", {userName: name}, "#/user/"+name);
}

function loadSpecDetail(sId){

	$('#me_specs tr').each(function(index) {
		if($(this).attr('id') == "tr_"+sId){
			$(this).addClass("spec-detail-list-selected");
		}else{
			$(this).removeClass("spec-detail-list-selected");
		}
	});
	$('#spec-detail-content').fadeOut('fast' , function(){
		$('#spec-detail-content').html('<div class=\"center\"><img class=\"spec-detail-loader-image\" src=\"media/loader.gif\"></img></div>');
		$('#spec-detail-content').fadeIn('fast');
		$("#spec-detail-content").load("spec_detail.php", {specId: sId}, function(response, status, xhr) {
			if (status == "error") {
				var msg = "Sorry but there was an error: ";
				$("#spec-detail-content").html(msg + xhr.status + " " + xhr.statusText);
			}else{
			//success
				//$("#spec-detail-content").html(response);
				$("#spec-detail-content").hide();
				$("#spec-detail-content").fadeIn('fast');
			}
		});
	});
}

function loadSpecList(uName){
	$('#spec-list').html('<div class=\"center\"><img class=\"spec-list-loader-image\" src=\"media/loader.gif\"></img></div>');
	$('#spec-list').load("spec_list.php" ,{userName: uName} , function(response, status, xhr) {
	  if (status == "error") {
	    var msg = "Sorry but there was an error: ";
	    $('#spec-list').html(msg + xhr.status + " " + xhr.statusText);
	  }else{
		$("#spec-list").hide();
		$("#spec-list").fadeIn('fast');
	  }
	});
}

function loadSpecComments(sId, am){
	$("#spec-comments-out").css({height: $("#spec-comments").height()});
	// fade out inner div
	$("#spec-comments").fadeOut('fast' , function(){
		// change the div content
		$('#spec-comments').html("<div class=\"center\"><img class=\"spec-comment-loader-image\" src=\"media/loader.gif\"></img></div>");
		$("#spec-comments").fadeIn('fast');
		
		$("#spec-comments").load("spec_comment.php" ,{specId: sId, amount: am} , function(response, status, xhr) {
		  if (status == "error") {
			var msg = "Sorry but there was an error: ";
			$('#spec-comments').html(msg + xhr.status + " " + xhr.statusText);
		  }else{
			$("#spec-comments-out").animate({height: $("#spec-comments").height()}, function(){
				// show the inner div
				$("#spec-comments").fadeIn('fast');
			});
		  }
		});
	});
}
function deleteComment(cId, sId){
	$.post("php/deleteComment.php", {commentId: cId},
		function(data){
			if(data.status == "success"){
				//$('#tr_'+cId).remove();
				loadSpecComments(sId, 5);
			}else{
				var msg = "Sorry but there was an error: ";
				$("#spec-detail-content").html(msg + xhr.status + " " + xhr.statusText);
			}
		   }, "json");
}

function writeComment(sId, txt){
	var content = $('#spec-comment-write-container').html();
	$('#spec-comment-write-container').html('<div class=\"center\"><img class=\"spec-write-comment-loader-image\" src=\"media/loader.gif\"></img></div>');
	$.post("php/saveComment.php", {specId: sId, comment: txt},
		function(data){
			if(data.status == "success"){
				loadSpecComments(sId, 5);
			}else{
				$('#spec-comment-write-container').html(content);
			}
		   }, "json");
}